package exam.controller;

import com.github.pagehelper.PageInfo;
import exam.bean.Teacher;
import exam.service.ITeacherService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teachers")
public class TeacherController {

    @Resource
    private ITeacherService teacherService;

    @Value("${upload.uploadPath}")
    private String uploadPath;

    @RequestMapping(value = "/teacher", method = {RequestMethod.POST,RequestMethod.GET})
    public String queryTeachers(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                    int pageNum,
                                @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                    int pageSize, @RequestParam Map<String,Object> queryMap, Model model, HttpServletRequest request){

        List<Teacher> teacherList = teacherService.selectTeacher(pageNum, pageSize,queryMap);

        model.addAttribute("pageInfo",new PageInfo<>(teacherList));
        model.addAttribute("teacherList",teacherList);
        model.addAttribute("queryMap",queryMap);

        return "indexTeacher";
    }

    // 获取教师列表
    @ResponseBody
    @GetMapping("/list")
    public List<Teacher> list(){
        return teacherService.selectTeacher(1,10,null);
    }

    @ResponseBody
    @PostMapping("/add")
    public boolean add(Teacher teacher){
        return teacherService.insertTeacher(teacher,uploadPath)>0?true:false;
    }

    @GetMapping("/toAdd")
    public String toAdd(){
        return "addTeacher";
    }

    @GetMapping("/toDetail")
    public String toDetail(int id, Model model){

        Map<String,Object> map=new HashMap<>();
        map.put("id", id);

        Teacher teacher = teacherService.selectTeacherSingle(map);

        model.addAttribute("teacher",teacher);

        return "detailTeacher";


    }

    @GetMapping("/toUpdate")
    public String toUpdate(int id, Model model){
        Map<String,Object> map=new HashMap<>();
        map.put("id", id);

        Teacher teacher = teacherService.selectTeacherSingle(map);

        model.addAttribute("teacher",teacher);


        return "updateTeacher";
    }

    @ResponseBody
    @PostMapping("/update")
    public boolean update(Teacher teacher){
        return teacherService.updateTeacher(teacher,uploadPath)>0?true:false;
    }

    @ResponseBody
    @GetMapping("/delete")
    public boolean delete(int id){
        return teacherService.deleteTeacher(id)>0?true:false;
    }


    /**
     * 附件下载
     * @param response
     * @param fileName
     *          混淆后的文件名
     * @param fileNameSrc
     *          原文件名
     */
    @GetMapping("downloadFile")
    public void downloadFile(HttpServletResponse response,String fileName,String fileNameSrc){

        FileUtil.downloadFile(response, uploadPath, fileName, fileNameSrc);

    }


}
